package com.yf.notify.modules.sms.factory;

import com.yf.ability.config.dto.CfgPropDTO;
import com.yf.ability.config.enums.ConfigType;
import com.yf.ability.config.enums.ProviderType;
import com.yf.ability.config.service.CfgPropService;
import com.yf.base.api.exception.ServiceException;
import com.yf.notify.modules.sms.service.SmsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class SmsFactory {

    @Autowired
    private SmsService aliyunSmsService;

    @Autowired
    private SmsService qcloudSmsService;

    @Autowired
    private CfgPropService cfgPropService;


    /**
     * 获取工厂实例
     * @return
     */
    public SmsService getService(){

        // 从缓存中获取配置
        CfgPropDTO cfg = cfgPropService.detail(ConfigType.SMS, null);

        if(cfg == null){
            throw new ServiceException("找不到默认的短信发送配置，请确认！");
        }

        if(ProviderType.ALIYUN.equalsIgnoreCase(cfg.getProvider())){
            return aliyunSmsService;
        }

        if(ProviderType.QCLOUD.equalsIgnoreCase(cfg.getProvider())){
            return qcloudSmsService;
        }

        throw new ServiceException("短信服务商配置不正确！");
    }
}
